package com.aiyige.utils.widget.articleview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.aiyige.R;
import com.aiyige.page.publish.info.model.TextNode;
import com.aiyige.utils.widget.BaseBottomDialogFragment;
import com.aiyige.utils.widget.articleview.model.ArticleNode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zhuyakun on 2017/11/13.
 */

public class EditTextStyleBottomDialog extends BaseBottomDialogFragment {

    @BindView(R.id.bigTitleSelectIcon)
    ImageView bigTitleSelectIcon;
    @BindView(R.id.bigTitleLayout)
    RelativeLayout bigTitleLayout;
    @BindView(R.id.smallTitleSelectIcon)
    ImageView smallTitleSelectIcon;
    @BindView(R.id.smallTitleLayout)
    RelativeLayout smallTitleLayout;
    @BindView(R.id.mainBodySelectIcon)
    ImageView mainBodySelectIcon;
    @BindView(R.id.mainBodyLayout)
    RelativeLayout mainBodyLayout;
    Unbinder unbinder;
    int position;
    int textStyle;
    Listener listener;

    public Listener getListener() {
        return listener;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public static EditTextStyleBottomDialog newInstance() {
        return new EditTextStyleBottomDialog();
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public int getTextStyle() {
        return textStyle;
    }

    public void setTextStyle(int textStyle) {
        this.textStyle = textStyle;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.edit_text_style_bottom_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        switch (textStyle) {
            case ArticleNode.TEXT_STYLE_BIG_TITLE:
                bigTitleSelectIcon.setVisibility(View.VISIBLE);
                break;
            case ArticleNode.TEXT_STYLE_SMALL_TITLE:
                smallTitleSelectIcon.setVisibility(View.VISIBLE);
                break;
            case ArticleNode.TEXT_STYLE_MAIN_BODY:
                mainBodySelectIcon.setVisibility(View.VISIBLE);
                break;
        }
        return view;
    }

    public void clearSelectIcon() {
        bigTitleSelectIcon.setVisibility(View.INVISIBLE);
        smallTitleSelectIcon.setVisibility(View.INVISIBLE);
        mainBodySelectIcon.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.bigTitleLayout, R.id.smallTitleLayout, R.id.mainBodyLayout})
    public void onViewClicked(View view) {
        clearSelectIcon();
        switch (view.getId()) {
            case R.id.bigTitleLayout:
                bigTitleSelectIcon.setVisibility(View.VISIBLE);
                if (listener != null) {
                    listener.onSelect(position, ArticleNode.TEXT_STYLE_BIG_TITLE);
                }
                break;
            case R.id.smallTitleLayout:
                smallTitleSelectIcon.setVisibility(View.VISIBLE);
                if (listener != null) {
                    listener.onSelect(position, ArticleNode.TEXT_STYLE_SMALL_TITLE);
                }
                break;
            case R.id.mainBodyLayout:
                mainBodySelectIcon.setVisibility(View.VISIBLE);
                if (listener != null) {
                    listener.onSelect(position, ArticleNode.TEXT_STYLE_MAIN_BODY);
                }
                break;
        }
        dismiss();
    }

    public interface Listener {
        void onSelect(int position, int textStyle);
    }
}
